﻿using System.Diagnostics;
using System.Threading;

namespace MadLibrary.Core.Logging
{
    /// <summary>
    /// Trace logger
    /// </summary>
    public class TraceLogger : ILog
    {
        private readonly int _threadId;

        /// <summary>
        /// Constructor
        /// </summary>
        public TraceLogger()
        {
            _threadId = Thread.CurrentThread.ManagedThreadId;
        }

        /// <summary>
        /// Debug
        /// </summary>
        /// <param name="message">Message.</param>
        public void Debug(string message)
        {
            Log(string.Format("[{0}] DGB: {1}", _threadId, message));
        }

        /// <summary>
        /// Info
        /// </summary>
        /// <param name="message">Message.</param>
        public void Info(string message)
        {
            Log(string.Format("[{0}] INFO: {1}", _threadId, message));
        }

        /// <summary>
        /// Warning
        /// </summary>
        /// <param name="message">Message.</param>
        public void Warning(string message)
        {
            Log(string.Format("[{0}] WRN: {1}", _threadId, message));
        }

        /// <summary>
        /// Fatal error
        /// </summary>
        /// <param name="message">Message.</param>
        public void Fatal(string message)
        {
            Log(string.Format("[{0}] ERR: {1}", _threadId, message));
        }

        private static void Log(string message)
        {
            if(message.EndsWith("\r\n"))
                message = message.Substring(0, message.Length - 2);

            Trace.WriteLine(message);
        }
    }
}